package com.jcl.cloud.services.login.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class CustomJedisPool {
    private static Logger logger = LoggerFactory.getLogger(CustomJedisPool.class);
    private static volatile JedisPool jedisPool;


    static {
        if (jedisPool == null) {
            synchronized (JedisPool.class) {
                if (jedisPool == null) {
                    init();
                }
            }
        }
    }

    private static void init() {
        try {
            JedisPoolConfig config = new JedisPoolConfig();
            config.setMaxTotal(100);
            config.setMaxIdle(10);
            config.setMaxWaitMillis(3000);
            config.setTestOnBorrow(true);
            int TIMEOUT = 10000;
            //121.43.163.88        //172.16.115.80
            jedisPool = new JedisPool(config, "172.16.115.80", 8083, TIMEOUT);
        } catch (Exception e) {
            logger.error("初始化JedisPool发生异常；", e);
        }
    }


    public static Jedis getResource() {
        return jedisPool.getResource();
    }


    public static void returnResource(Jedis jedis) {
        try {
            jedisPool.returnResource(jedis);
        } catch (Exception e) {
            logger.error("Jedis对象归还发生异常");
        }
    }


}
